<%!
public class RS
{
	java.sql.Statement stm1=null;
	java.sql.ResultSet rs=null;
	public int colCount=0;

	public RS(java.sql.Statement stm1)
	{
		this.stm1=stm1;
	}
	public String parseTable()
	{

		String str1="";
		int rowid=0;
		StringBuffer re1=new StringBuffer();
		re1.append("<table style=display:inline class=t4 cellpadding=0 cellspacing=1 style=border-collapse:collapse border=1><tr><td>");
		for(int i=1;i<=colCount;i++)
		{
			if(i!=1) re1.append("</td><td>");
			try{
			re1.append(rs.getMetaData().getColumnName(i));
			}catch(Exception e)
			{}
		}//for
		try{
		while(rs.next())
		{
			re1.append("</tr><tr><td>");
			rowid++;
			for(int i=1;i<=colCount;i++)
			{
			if(i!=1) re1.append("</td><td>");
			str1=rs.getString(i);
			if(str1!=null)
			{
			//str1=str1.replaceAll("\\\\","\\\\\\\\").replaceAll("\"","\\\\\"").replaceAll("\t","\\\\\t").replaceAll("\r\n","\\\\r\\\\n").replaceAll("\n","\\\\n");
			re1.append(str1);
			}

			}

		}
		}catch(Exception e)
		{}
		re1.append("</tr></table>");
		return re1.toString();
	}
	public String parseXML()
	{
		String str1="";
		int rowid=0;
		StringBuffer re1=new StringBuffer();
		for(int i=1;i<=colCount;i++)
		{
			if(i!=1) re1.append("\t");
			try{
			re1.append(rs.getMetaData().getColumnName(i));
			}catch(Exception e)
			{}
		}//for
		try{
		while(rs.next())
		{
			re1.append("\r\n");
			rowid++;
			for(int i=1;i<=colCount;i++)
			{
			if(i!=1) re1.append("\t");
			str1=rs.getString(i);
			if(str1!=null)
			{
			str1=str1.replaceAll("\\\\","\\\\\\\\").replaceAll("\"","\\\\\"").replaceAll("\t","\\\\\t").replaceAll("\r\n","\\\\r\\\\n").replaceAll("\n","\\\\n");
			re1.append(str1);
			}

			}

		}
		}catch(Exception e)
		{}
		return re1.toString();
	}
	public void open(String str1) throws Exception
	{
		 this.query(str1);

		}
	public void open(String str1,int limit1,int limit2)  throws Exception
	{
		 this.query(str1,limit1,limit2);

		}
	public void query(String str1)  throws Exception
	{
		try{
			//System.out.println(str1);
			rs=stm1.executeQuery(str1);
			colCount=rs.getMetaData().getColumnCount();

			}
		catch(Exception e)
		{
			rs=null;
			System.out.println(e.toString());
			throw e;

			}
	}
	public void query(String str1,int limit1,int limit2)  throws Exception
	{
		try{
			str1+=" limit "+String.valueOf(limit1)+","+String.valueOf(limit2);
			//System.out.println(str1);

			rs=stm1.executeQuery(str1);
			colCount=rs.getMetaData().getColumnCount();

			}
		catch(Exception e)
		{
			rs=null;
			System.out.println(e.toString());throw e;

			}
		}
	public void cmd(String str1)  throws Exception
	{
		try{
			//System.out.println(str1);
			stm1.execute(str1);

			}
			catch(Exception e)
			{
				System.out.println(e.toString());throw e;
			}
		}
	public boolean next()  throws Exception
	{
		try{
		if(rs!=null)
		{
			return rs.next();
		}else
		{
			return false;
		}
		}catch(Exception e)
		{throw e;
			}
	}
	public String getString(String str1)  throws Exception
	{
		return this.get(str1);
	}
	public String get(int str1)  throws Exception
	{
		String tmp1;
		try{
		tmp1=rs.getString(str1)==null?"":rs.getString(str1);
		}catch(Exception e)
		{
			throw e;
			}
		return tmp1;
		}
	public String get(String str1)  throws Exception
	{
		String tmp1;
		try{
		tmp1=rs.getString(str1)==null?"":rs.getString(str1);
		}catch(Exception e)
		{
			tmp1=e.toString();
			throw e;
			}
		return tmp1;
		}
	public int getInt(String str1)  throws Exception
	{
		try{
			return rs.getInt(str1);
			}catch(Exception e)
			{
				throw e;
				}
	}
	public String getColName(int i)  throws Exception
	{	try{
		return rs.getMetaData().getColumnName(i);
		}catch(Exception e)
		{
			throw e;
			}
		}
	public void close()  throws Exception
	{
		try{
			stm1.close();
		}catch(Exception e)
		{throw e;}
		}

}//class Stm
public class Conn
{
   public java.sql.Connection conn = null;
   boolean status=false;
   public String err="";
   String defaultDb="oracle";
   public Conn() throws Exception
   {
   	try{
  javax.naming.Context ctx=new javax.naming.InitialContext();
	javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookup("java:comp/env/jdbc/"+this.defaultDb);
	conn=ds.getConnection();
	 	conn.setAutoCommit(false);
   	status=true;
   	}catch(Exception e)
   {
   	status=false;
   	err=e.toString();
   	throw e;
   }
	}
   public Conn(String datasource) throws Exception
   {

   try{
  javax.naming.Context ctx=new javax.naming.InitialContext();
  if(datasource.equals("")) datasource=defaultDb;
	javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookup("java:comp/env/jdbc/"+datasource);
	conn=ds.getConnection();
	 	conn.setAutoCommit(false);
   	status=true;
   	}catch(Exception e)
   {
   	status=false;
   	err=e.toString();
   	throw e;
   }
   }//new
   public RS getRS() throws Exception
   {
   	try{
		return new RS(conn.createStatement());
		}catch(Exception e)
		{
		throw e;
		}
   	}
   public java.sql.Statement createStatement()  throws Exception
	{
		try{
		return conn.createStatement();
		}catch(Exception e)
		{
		throw e;
		}
	}
	public void close()  throws Exception
	{
		try{
		conn.commit();
		conn.close();
		status=false;
		}catch(Exception e)
		{throw e;}
	}
	public void commit()  throws Exception
	{
		try{
		conn.commit();
		}catch(Exception e)
		{throw e;}
	}
	public void rollback()  throws Exception
	{
		try{
		conn.rollback();
		}catch(Exception e)
		{throw e;}
	}
}//class sqlca
public class SQL
{
	public java.util.ArrayList filters=new java.util.ArrayList();
	public java.util.ArrayList cols=new java.util.ArrayList();
	public java.util.ArrayList values=new java.util.ArrayList();
	public String tablename="";
	SQL()
	{}
	SQL(String tablename)
	{
	this.tablename=tablename;
	}
	public void setTable(String str1) throws Exception
	{
		this.tablename=str1;
	}
	public void addFilter(String str1) throws Exception
	{
	  	this.filters.add(str1);
	}
	public void set(String str1,String str2) throws Exception
	{
		this.cols.add(str1);
		this.values.add(str2);
	}
	public String getSql(String cmd)
	{
	  String strsql="";
	  if(cmd.equals("select"))
	  {
	  strsql="select * from "+this.tablename;

	 strsql+=" where 1=1 ";
	 for(int i=0;i<this.filters.size();i++)
	 strsql+=" and "+(String)this.filters.get(i);

	  }
	    if(cmd.equals("insert"))
	    {
	    strsql="insert into "+this.tablename+"(";
	 for(int i=0;i<this.cols.size();i++)
	 strsql+=i==0?(String)this.cols.get(i):","+(String)this.cols.get(i);
	 strsql+=") values(";
	 for(int i=0;i<this.values.size();i++)
	 strsql+=i==0?(String)this.values.get(i):","+(String)this.values.get(i);
	 strsql+=")";
	    }
	      if(cmd.equals("update"))
	      {
	      strsql="update "+this.tablename+" set ";
	 for(int i=0;i<this.cols.size();i++)
	 strsql+=i==0?(String)this.cols.get(i)+"="+(String)this.values.get(i):","+(String)this.cols.get(i)+"="+(String)this.values.get(i);

	 for(int i=0;i<this.filters.size();i++)
	 strsql+=i==0?" where "+(String)this.filters.get(i):" and "+(String)this.filters.get(i);
	      }
	        if(cmd.equals("delete"))
	        {
	        strsql="delete from "+this.tablename;

	  for(int i=0;i<this.filters.size();i++)
	 strsql+=i==0?" where "+(String)this.filters.get(i):" and "+(String)this.filters.get(i);
	        }
	  return strsql;
	}
	public void reset()
	{
	 this.filters=new java.util.ArrayList();
	 this.values=new java.util.ArrayList();
	 this.cols=new java.util.ArrayList();
	 this.tablename="";
	}
}
%>
